/* -----------------
* brief
* 1. this is code gen by tools
 */

package service

import (
	"fmt"
	"github.com/mjiulee/lego"
	"saas_sys_base/common"
	. "saas_sys_base/modules/product/models"
)

type ProductService struct {
	// add your model here
	tbProduct Product
}

func (t *ProductService) ListProduct(keyword string, page, psize int, orderField, order string) ([]map[string]string, int64, error) {
	// 业务查询
	queryMap := map[string]interface{}{
		"table":  map[string]string{"table": "tb_product", "alias": "t1"},
		"fields": []string{"t1.*", "t2.shop_name", "t3.name as tenant_name"},
		"join": []map[string]string{
			map[string]string{"table": "tb_e_shop as t2", "on": "t2.id = t1.e_shop_id"},
			map[string]string{"table": "tb_tenant as t3", "on": "t3.id = t1.tenant_id"},
		},
		"where": []lego.QMapWhereField{
			{"t1.if_del", "=", 0, "AND", false},
		},
		"search": map[string]string{
			"t1.product_name": keyword,
		},
		"orders": []string{"t1.id asc"},
		"page": map[string]int{
			"page":  page,
			"psize": psize,
		},
	}

	if orderField != "" && order != "" {
		orderStr := fmt.Sprintf("t1.%s %s", orderField, order)
		queryMap["orders"] = []string{orderStr, "t1.cdate asc"}
	}

	return lego.QueryListByMap(queryMap)
}

func (t *ProductService) ListAppAttrByWho(who *common.Who, keyword string, page, psize int) ([]map[string]string, int64, error) {
	// 业务查询
	queryMap := map[string]interface{}{
		"table":  map[string]string{"table": "tb_app_attr", "alias": "t1"},
		"fields": []string{"t1.*"},
		"join": []map[string]string{
			map[string]string{"table": "tb_app_tenant_permission as t2", "on": "t2.mini_app_id = t1.mini_app_id"},
		},
		"where": []lego.QMapWhereField{
			{"t1.if_del", "=", 0, "AND", false},
			//{"xx.tenant_id", "=", who.TenantId, "AND", false},
			//{"xx.mini_app_id", "=", who.MiniAppId, "AND", false},

		},
		"search": map[string]string{
			//"t1.id": req.Keyword,
		},
		"orders": []string{"t1.id asc"},
		"page": map[string]int{
			"page":  page,
			"psize": psize,
		},
	}

	/*{ //这段生成代码的目的是，当需要根据条件判断，来增加对应where条件时，使用这里的逻辑
		queryWhere := queryMap["where"].([]lego.QMapWhereField)
		if who.TenantId != 0 {
			queryWhere = append(queryWhere, lego.QMapWhereField{"t2.tenant_id", "=", who.TenantId, "AND", false})
		}

		if who.MiniAppId != 0 {
			queryWhere = append(queryWhere, lego.QMapWhereField{"t2.mini_app_id", "=", who.MiniAppId, "AND", false})
		}

	}*/

	return lego.QueryListByMap(queryMap)
}

/* 根据id获取记录
 * -----------------
 */
func (t *ProductService) GetProductById(id int64) *Product {
	return t.tbProduct.RecodeById(id)
}

/* 根据id获取记录
 * -----------------
 */
func (t *ProductService) GetTenantProductById(id,shopId int64) *Product {
	return t.tbProduct.TenantRecodeById(id,shopId)
}

/* 添加
 * -----------------
 */
func (t *ProductService) SaveProduct(item2add *Product) bool {
	return t.tbProduct.AddRecode(item2add)
}

/* 删除(硬删除)*/
func (t *ProductService) DelProductById(id int64) bool {
	return t.tbProduct.DelRecodeById(id)
}

/* 更新
 * -----------------*/
func (t *ProductService) UpdateProduct(rc *Product) bool {
	return t.tbProduct.UpdateRecode(rc)
}


/*租户端*/

func (t *ProductService) ListProductByTenant(keyword string,tenantId int64, page, psize int, orderField, order string, cateId string) ([]map[string]string, int64, error) {
	// 业务查询
	queryMap := map[string]interface{}{
		"table":  map[string]string{"table": "tb_product", "alias": "t1"},
		"fields": []string{"t1.*", "t2.shop_name"},
		"join": []map[string]string{
			map[string]string{"table": "tb_e_shop as t2", "on": "t2.id = t1.e_shop_id"},
		},
		"where": []lego.QMapWhereField{
			{"t1.if_del", "=", 0, "AND", false},
			{"t2.tenant_id", "=", tenantId, "AND", false},
		},
		"search": map[string]string{
			"t1.product_name": keyword,
			"t1.cate_id": cateId,
		},
		"orders": []string{"t1.id asc"},
		"page": map[string]int{
			"page":  page,
			"psize": psize,
		},
	}

	if orderField != "" && order != "" {
		orderStr := fmt.Sprintf("t1.%s %s", orderField, order)
		queryMap["orders"] = []string{orderStr, "t1.cdate asc"}
	}

	return lego.QueryListByMap(queryMap)
}

/*商城端*/

func (t *ProductService) ListProductByMall(keyword string,shopId int64, page, psize int, orderField, order string) ([]map[string]string, int64, error) {
	// 业务查询
	queryMap := map[string]interface{}{
		"table":  map[string]string{"table": "tb_product", "alias": "t1"},
		"fields": []string{"t1.*"},
		"join": []map[string]string{
			//map[string]string{"table": "tb_e_shop as t2", "on": "t2.id = t1.e_shop_id"},
		},
		"where": []lego.QMapWhereField{
			{"t1.if_del", "=", 0, "AND", false},
			{"t1.if_sale", "=", 1, "AND", false},
			{"t1.e_shop_id", "=", shopId, "AND", false},
		},
		"search": map[string]string{
			"t1.product_name": keyword,
		},
		"orders": []string{"t1.id asc"},
		"page": map[string]int{
			"page":  page,
			"psize": psize,
		},
	}

	if orderField != "" && order != "" {
		orderStr := fmt.Sprintf("t1.%s %s", orderField, order)
		queryMap["orders"] = []string{orderStr, "t1.cdate asc"}
	}

	return lego.QueryListByMap(queryMap)
}